$(function(){
    var ajaxFill = new Z.AjaxFill({
	url: Z.url('/backend/entry/records',true),
	templates: {
	    list: $('#list-template'),
	    none: '<tr><td colspan="5" style="text-align:center;">记录为空</td></tr>',
	    loading: '<tr><td colspan="5" style="text-align:center;"><img src="/assets/img/backend/z-ajax-loading.gif"/></td></tr>'
	},
	containers: {
	    list: $('table.records tbody'),
	    pageLinks: $('table.records tfoot td:eq(1)')
	},
	data: {
	    type: TYPE,
	    pageSize: 10
	}
    }),
    editor = new baidu.editor.ui.Editor({
	textarea: 'content'
    }),
    editor_en = new baidu.editor.ui.Editor({
	textarea: 'content_en'
    }),
    getId = function(element){
	return Z.parseInt($(element).parent().siblings().first().find(':checkbox').val());
    };
    
    
    ajaxFill.request();

    editor.render('content');
    editor_en.render('content_en');
    
    Z.tab($('ul.tab-button a'),$('.tab-content'),['active','']);
    
    $('form').hide();
    
    $('a.delete').live('click',function(){
	var id = getId($(this));
	Z.confirm({
	    content: 'r u sure?',
	    onConfirm: function(){
		Z.ajax({
		    url: Z.url('/backend/entry/delete/'+id,true),
		    success: function(){
			ajaxFill.reload();
		    }
		});
	    }
	});
	return false;
    });
    
    $('a.hide').live('click',function(){
	var id = getId($(this));
	Z.ajax({
	    url: Z.url('/backend/entry/hide/'+id,true),
	    success: function(){
		ajaxFill.reload();
	    }
	});
	return false;
    });
    
    $('a.select-all').click(function(){
	$('table.records tbody td input:checkbox:not(:checked)').trigger('click');
    });
    
    $('a.select-reverse').click(function(){
	$('table.records tbody td input:checkbox').trigger('click');
    });
    
    $('a.delete-batch').click(function(){
	var ids = [];
	$('table.records tbody td input:checkbox:checked').each(function(){
	    ids.push(Z.parseInt($(this).val()));
	});
	Z.confirm({
	    content: '确定删除多个?',
	    onConfirm: function(){
		Z.ajax({
		    url: Z.url('/backend/entry/delete_batch',true),
		    data: {
			ids:ids
		    },
		    success: function(){
			ajaxFill.reload();
		    }
		});
	    }
	});
    });
    
    $('a.add').click(function(){
	$('table.records').hide();
	$('form').show().attr('action',$('form').attr('action').replace(/\/update$/,'/insert'));
	$('input[name=title]').val('');
	$('input[name=album]').val(0);
	editor.setContent('');
    });
    
    $('a.edit').live('click',function(){
	$('table.records').hide();
	$('form').show().attr('action',$('form').attr('action').replace(/\/insert$/,'/update'));
	var id = getId($(this));
	Z.ajax({
	    url: Z.url('/backend/entry/find/'+id,true),
	    success: function(data){
		Z.fillForm($('form'),data);
		editor.setContent(data.content);
		editor_en.setContent(data.content_en);
	    }
	});
	return false;
    });
    
    $('a.back').click(function(){
	$('table.records').show();
	$('form').hide();
    });
    
    //form -start
    var uploadifySetData = function(){
	$('#photo-uploader').uploadifySettings('scriptData', {
	    'album':$('input[name=album]').val(),
	    'thumb_width': $('input[name=thumb_width]').val(),
	    'thumb_height': $('input[name=thumb_height]').val(),
	    'user':USER_ID,
	    'albumName': 'temp_name_'+TYPE,
	    'albumDescription': TYPE
	});
    };
    
    $('#photo-uploader').uploadify({
	//'buttonImg': Z.url('/assets/images/select_button_bg.png'),
	'uploader'  : Z.url('/assets/js/uploadify/uploadify.swf'),
	'script'    :  Z.url('/assets/js/uploadify/photo_upload.php'),
	'cancelImg' : Z.url('/assets/js/uploadify/cancel.png'),
	'folder'    : '/assets/uploads',
	'fileDataName': 'photo_upload',
	'fileExt'     : '*.jpg;*.gif;*.png;*.bmp;*.jpeg;*.JPG;*.GIF;*.PNG;*.BMP;*.JPEG',
	'sizeLimit'   : 2 * 1024 * 1024,
	'queueSizeLimit': 10,
	'multi' :  true,
	'auto'  : false,
	'onSelectOnce' : function(){
	    uploadifySetData();
	},
	'onComplete' : function(event,id,fileObject,response,data){
	    Z.log(response);
	    var responseJson;
	    eval('responseJson = '+response+';');
	    $('input[name=album]').val(responseJson.data.album);
	    uploadifySetData();
	},
	onError: function(event,id,fileObject,errorObject){
	    Z.log(arguments);
	}
    });
    
    $('#photo-uploader-button').click(function(){
	$('#photo-uploader').uploadifyUpload();
	return false;
    });
    
    $('#photo-uploader-clean').click(function(){
	return false;
    });
    //form -end
    
    $('form').submit(function(){
	var album = $('input[name=album]').val();
	if(Z.parseInt(album) == 0){
	    Z.alert('您尚未上传图片');
	    return false;
	}
	editor.sync();
	editor_en.sync();
	Z.ajax({
	    url: $(this).attr('action'),
	    type: $(this).attr('method'),
	    data: $(this).serialize(),
	    success: function(){
		ajaxFill.reload();
	    }
	});
	return false;
    });
});